package com.platform.controller;

import com.platform.dto.OrderExcelDTO;
import com.platform.entity.OrderEntity;
import com.platform.entity.OrderGoodsEntity;
import com.platform.entity.UserEntity;
import com.platform.service.OrderGoodsService;
import com.platform.service.OrderService;
import com.platform.utils.PageUtils;
import com.platform.utils.Query;
import com.platform.utils.R;
import com.platform.utils.SmsUtil;
import com.platform.utils.excel.ExcelExport;
import org.apache.commons.httpclient.util.DateParseException;
import org.apache.commons.httpclient.util.DateUtil;
import org.apache.commons.lang.StringEscapeUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.*;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import java.beans.PropertyEditorSupport;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.IOException;
import java.math.BigDecimal;
import java.util.*;


/**
 * @author lipengjun
 * @email 939961241@qq.com
 * @date 2017-08-13 10:41:09
 */
@RestController
@RequestMapping("order")
public class OrderController {
    @InitBinder
    protected void initBinder(WebDataBinder binder) {
        // Date 类型转换
        binder.registerCustomEditor(Date.class, new PropertyEditorSupport() {
            @Override
            public void setAsText(String text) {
                try {
                    setValue(DateUtil.parseDate(text));
                } catch (DateParseException e) {
                    e.printStackTrace();
                }
            }
        });
    }

    @Autowired
    private OrderService orderService;

    @Autowired
    private OrderGoodsService orderGoodsService;

    /**
     * 列表
     */
    @RequestMapping("/list")
    @RequiresPermissions("order:list")
    public R list(@RequestParam Map<String, Object> params) {
        // 查询列表数据
        Query query = new Query(params);

        List<OrderEntity> orderList = orderService.queryList(query);
        int total = orderService.queryTotal(query);

        PageUtils pageUtil = new PageUtils(orderList, total, query.getLimit(), query.getPage());

        return R.ok().put("page", pageUtil);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{id}")
    @RequiresPermissions("order:info")
    public R info(@PathVariable("id") Integer id) {
        OrderEntity order = orderService.queryObject(id);

        return R.ok().put("order", order);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    @RequiresPermissions("order:save")
    public R save(@RequestBody OrderEntity order) {
        orderService.save(order);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    @RequiresPermissions("order:update")
    public R update(@RequestBody OrderEntity order) {
        orderService.update(order);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    @RequiresPermissions("order:delete")
    public R delete(@RequestBody Integer[] ids) {
        orderService.deleteBatch(ids);

        return R.ok();
    }

    /**
     * 查看所有列表
     */
    @RequestMapping("/queryAll")
    public R queryAll(@RequestParam Map<String, Object> params) {

        List<OrderEntity> list = orderService.queryList(params);

        return R.ok().put("list", list);
    }

    /**
     * 总计
     */
    @RequestMapping("/queryTotal")
    public R queryTotal(@RequestParam Map<String, Object> params) {
        int sum = orderService.queryTotal(params);

        return R.ok().put("sum", sum);
    }

    /**
     * 确定收货
     *
     * @param id
     * @return
     */
    @RequestMapping("/confirm")
    @RequiresPermissions("order:confirm")
    public R confirm(@RequestBody Integer id) {
        orderService.confirm(id);

        return R.ok();
    }

    /**
     * 发货
     *
     * @param order
     * @return
     */
    @RequestMapping("/sendGoods")
    @RequiresPermissions("order:sendGoods")
    public R sendGoods(@RequestBody OrderEntity order) {
        orderService.sendGoods(order);
        return R.ok();
    }


    @RequestMapping("/export")
    @RequiresPermissions("order:list")
    public R export(@RequestParam Map<String, Object> params, HttpServletResponse response) throws IOException {
        List<OrderExcelDTO> orderGoods = orderGoodsService.export(params);

        ExcelExport ee = new ExcelExport("订单列表");

        String[] header = new String[]{"订单编号", "商品名称", "商品型号", "商品单价", "商品数量", "商品总价", "支付时间", "下单人ID",
        "下单人姓名", "推荐人ID", "推荐人姓名", "收货姓名", "收货电话", "收货所在省", "收货所在市", "收货所在区", "收货地址",
        "订单创建时间", "订单状态", "发货快递", "发货快递号"};

        List<Map<String, Object>> list = new ArrayList<>();

        if (orderGoods != null && orderGoods.size() != 0) {
            for (OrderExcelDTO item : orderGoods) {
                LinkedHashMap<String, Object> map = new LinkedHashMap<>();
                map.put("OrderSn", item.getOrderSn());
                map.put("GoodsName", item.getGoodsName());
                map.put("GoodsSpecifitionNameValue", item.getGoodsSpecifitionNameValue());
                map.put("retailPrice", item.getRetailPrice());
                map.put("number", item.getNumber());
                map.put("totalPrice", item.getTotalPrice());
                map.put("payTime", item.getPayTime());
                map.put("orderUserId", item.getOrderUserId());
                map.put("orderNickname", item.getOrderNickname());
                map.put("referrerId", item.getReferrerId());
                map.put("referrerNickname", item.getReferrerNickname());
                map.put("consignee", item.getConsignee());
                map.put("mobile", item.getMobile());
                map.put("province", item.getProvince());
                map.put("city", item.getCity());
                map.put("district", item.getDistrict());
                map.put("address", item.getAddress());
                map.put("addTime", item.getAddTime());
                map.put("orderStatus", item.getOrderStatus());
                map.put("shippingName", item.getShippingName());
                map.put("shippingNo", item.getShippingNo());
                list.add(map);
            }
        }

        ee.addSheetByMap("订单列表", list, header);
        ee.export(response);
        return R.ok();
    }


}
